/**
 *Project: jh-framework
 *File: cn.xkshow.io.upload.SSHFtp.java 
 *Author: admin@xkshow.cn with the copyrite
 ****************************************************************
 *   VERSION       DATE            BY       CHANGE/COMMENT
 * --------------------------------------------------------------
 *    1.0        2013-1-27        xkshow.cn         create
 ****************************************************************
 *note:
 ***************************************************************/ 
package cn.xkshow.io.upload;

import java.io.File;
import java.io.IOException;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;

public class SSHFtp {
   private String userName;
   private String userPass;
   private String serverIp;
   private int serverPort;
   private Connection conn;
   private SCPClient client;
   private String pubKeyDir = null;
   public SSHFtp(String dir){
	   this.pubKeyDir = dir;
   }
   
   /**
    * 閸掓繂顬婇崠鏍懠閹猴拷
    * @throws IOException
    */
   public void connect() throws IOException{
	   conn = new Connection(serverIp, serverPort);
	   conn.connect();
	   File pubFile = new File(pubKeyDir + File.separator
				+ "pubkey" + File.separator + "id_rsa");
	   conn.authenticateWithPublicKey(userName, pubFile, userPass);
	   initClient();
   }
   
   /**
    * 閸掓繂顬婇崠鏈縧ient
    */
   private void initClient(){
	   client= new SCPClient(conn);
   }
   
   /**
    * 娑撳﹣绱堕弬鍥︽
    * @param localFile		閺堫剙婀撮弬鍥︽
    * @param remotePath		鏉╂粎鈻肩捄顖氱窞
    * @throws IOException
    */
   public void upload(String localFile,String remotePath) throws IOException{
	   client.put(localFile,remotePath);
   }
   
   /**
    * 娑撳娴囬弬鍥︽
    * @param reportFile		鏉╂粎鈻奸弬鍥︽
    * @param localPath		閺堫剙婀寸捄顖氱窞
    * @throws IOException
    */
   public void download(String reportFile,String localPath) throws IOException{
	   client.get(reportFile,localPath);
   }
   public void close(){
	   if(conn != null){  
      	 conn.close();  
       }  
   }
   
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		SSHFtp client=new SSHFtp("E:\\");
       	client.setServerIp("192.168.180.153");
       	client.setServerPort(18822);
       	client.setUserName("yysoft");
       	client.setUserPass("liuhaiqiang");
       	String localRealPath = "D:\\t1.gif";
       	String remoteRealPath = "/home/yysoft/SHOP_ADMIN/";
       	client.connect();
       	client.upload(localRealPath, remoteRealPath);
       	client.close();
       	client = null;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPass() {
		return userPass;
	}
	public void setUserPass(String userPass) {
		this.userPass = userPass;
	}
	public String getServerIp() {
		return serverIp;
	}
	public void setServerIp(String serverIp) {
		this.serverIp = serverIp;
	}
	public int getServerPort() {
		return serverPort;
	}
	public void setServerPort(int serverPort) {
		this.serverPort = serverPort;
	}

}
